package com.bjpowernode.service.impl;

import com.bjpowernode.constant.Constants;
import com.bjpowernode.manager.RedisManager;
import com.bjpowernode.mapper.TActivityMapper;
import com.bjpowernode.model.TActivity;
import com.bjpowernode.model.TUser;
import com.bjpowernode.query.ActivityQuery;
import com.bjpowernode.service.ActivityService;
import com.bjpowernode.util.CacheUtils;
import com.bjpowernode.util.JWTUtil;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import jakarta.annotation.Resource;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;
import java.util.concurrent.TimeUnit;

@Service
public class ActivityServiceImpl implements ActivityService {

    @Resource
    private TActivityMapper tActivityMapper;

    @Resource
    private RedisManager redisManager;

    @Override
    public PageInfo<TActivity> getActivityByPage(Integer current, ActivityQuery activityQuery) {
        //1.设置PageHelper
        PageHelper.startPage(current, Constants.PAGE_SIZE);
        //2.查询
        List<TActivity> list = tActivityMapper.selectActivityPage(activityQuery);
        //3.封装分页数据到PageInfo
        PageInfo<TActivity> info = new PageInfo<>(list);

        return info;
    }

    /**
     * 根据id查询市场活动详情信息
     *
     * @param id
     * @return
     */
    @Override
    public TActivity getActivityById(Integer id) {
        TActivity tActivity = tActivityMapper.selectActivityDetailById(id);
        return tActivity;
    }

    /**
     * 录入市场活动提交保存
     *
     * @param activityQuery
     * @return
     */
    @Override
    public int saveActivity(ActivityQuery activityQuery) {
        TActivity tActivity = new TActivity();

        //需要把ActivityQuery数据复制到TActivity对象中
        //spring提供了一个工具类：BeanUtils，它可以实现把一个对象的属性值复制到另一个对象中
        //复制的要求：源对象和目标对象，两个对象的成员变量名称相同，成员变量的类型也相同，这样才可以复制
        BeanUtils.copyProperties(activityQuery, tActivity);

        //设置一下创建时间
        tActivity.setCreateTime(new Date());

        //设置一下创建人
        Integer loginUserId = JWTUtil.parseUserFromJWT(activityQuery.getJwt()).getId();
        tActivity.setCreateBy(loginUserId);

        return tActivityMapper.insertSelective(tActivity);
    }

    /**
     * 编辑市场活动提交保存
     *
     * @param activityQuery
     * @return
     */
    @Override
    public int updateActivity(ActivityQuery activityQuery) {
        TActivity tActivity = new TActivity();

        //需要把ActivityQuery数据复制到TActivity对象中
        //spring提供了一个工具类：BeanUtils，它可以实现把一个对象的属性值复制到另一个对象中
        //复制的要求：源对象和目标对象，两个对象的成员变量名称相同，成员变量的类型也相同，这样才可以复制
        BeanUtils.copyProperties(activityQuery, tActivity);

        //设置一下编辑时间
        tActivity.setEditTime(new Date());

        //设置一下编辑人
        Integer loginUserId = JWTUtil.parseUserFromJWT(activityQuery.getJwt()).getId();
        tActivity.setEditBy(loginUserId);

        return tActivityMapper.updateByPrimaryKeySelective(tActivity);
    }

    @Override
    public int delActivityById(Integer id) {
        return tActivityMapper.deleteByPrimaryKey(id);
    }

    @Override
    public int batchDelActivityById(List<String> idList) {
        return tActivityMapper.deleteByIds(idList);
    }

    @Override
    public List<TActivity> getEffectiveActivity() {
        List<TActivity> tActivityList = CacheUtils.getCacheData(
                () -> (List<TActivity>)redisManager.getValue(Constants.REDIS_ACTIVITY_KEY),
                () -> tActivityMapper.selectEffectiveActivity(),
                (t) -> redisManager.setValue(Constants.REDIS_ACTIVITY_KEY, t, Constants.OPTIONS_EXPIRE_TIME, TimeUnit.MINUTES)
        );
        return tActivityList;
    }
}
